package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass= MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    //本地注入
    @Autowired
    private MenuDao menuDao;


    //菜单分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //获取一页几行
        Integer pageSize = queryPageBean.getPageSize();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取查找的字符串
        String queryString = queryPageBean.getQueryString();
        //利用分页助手
        PageHelper.startPage(currentPage,pageSize);


        //只查询查询自关联项为null
        //查询父类菜单信息
        Page<Menu> page = menuDao.findByConditionByNull(queryString);

        long total = page.getTotal();
        List<Menu> rows = page.getResult();

        //加一个判断方法---------------------------
        if(rows!=null&&rows.size()>0){

            for (Menu menu : rows) {
                List<Menu>children=new ArrayList<>();
                //根据父亲Id查询孩子Id
                List<Integer>childrenIds=menuDao.findMenuIdByMenuId(menu.getId());

                for (Integer childrenId : childrenIds) {
                    //建立List集合用来存储

                    //通过孩子Id查信息
                    Menu childrenmenu=menuDao.findChildren(childrenId);
                    //将子菜单信息封装进List集合
                    children.add(childrenmenu);

                }
                //将List封装进menu属性
                menu.setChildren(children);


            }
            return new PageResult(total,rows);

        }else {
            //调用dao层方法
             page = menuDao.findByCondition(queryString);
             total = page.getTotal();
             rows= page.getResult();

            return new PageResult(total,rows);
        }
        //--------------------------------------

    }



    //按Id 查询
    @Override
    public Menu findById(Integer id) {

        return menuDao.findById(id);
    }

    @Override
    public List< Map<String, Integer>>findParentId() {
        List<Map<String, Integer>>parentMenuIds= new ArrayList<>();
        List<Integer> findParentIds=menuDao.findParentId();

        for (Integer findParentId : findParentIds) {

            Map<String, Integer>map=new HashMap<>();
            map.put("text",findParentId);
            map.put("value",findParentId);
            parentMenuIds.add(map);
        }

        return parentMenuIds;
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }


}
